//
//  ByPersonalDataVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByPersonalDataVC.h"

/** 辅助 */
#import "ByIconChooseDataSource.h"              // 头像选择数据源

/** 控制器 */
#import "ByPersonalNickModifyVC.h"              // 昵称修改
#import "ByPersonalEmailModifyVC.h"             // 邮箱修改
#import "ByPersonalAuthenticationVC.h"          // 身份认证

#import "ByLoginVC.h"                           // 登录


#import "ByPersonalViewModel.h"                 // vm
@interface ByPersonalDataVC () <UITableViewDelegate, UITableViewDataSource, ByPersonalUploadIconDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) ByPersonalViewModel *vm;
@property (nonatomic, strong) UIImage *icon;

@property (nonatomic, strong) NSArray *subjects;
@property (nonatomic, strong) NSMutableArray *subjectDetails;
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource;

@end

@implementation ByPersonalDataVC

#pragma mark - getter
- (ByPersonalViewModel *)vm {
    if (!_vm) {
        _vm = [[ByPersonalViewModel alloc] init];
        _vm.uploadIconDelegate = self;
    }
    return _vm;
}
- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        _iconView.image = [UIImage imageWithColor:JM_THEME_COLOR];
        _iconView.layer.cornerRadius = _iconView.height / 2;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}
- (NSArray *)subjects {
    if (!_subjects) {
        _subjects = @[@"头像", @"昵称", @"邮箱", @"身份认证"];
    }
    return _subjects;
}
- (NSMutableArray *)subjectDetails {
    if (!_subjectDetails) {
        NSArray *temp = @[@"", @"未登录", @"未登录", @"未认证"];
        _subjectDetails = [[NSMutableArray alloc] initWithArray:temp];
    }
    return _subjectDetails;
}
- (ByIconChooseDataSource *)iconChooseDataSource {
    if (!_iconChooseDataSource) {
        _iconChooseDataSource = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource fireWithViewController:self];
    }
    return _iconChooseDataSource;
}

#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人资料";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBackBarButtonItem];
    
    [self createTableView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    /** 设置导航栏为半透明 状态栏为白色 */
    [self setNaviAttributeWithTranslucent:NO stateBarLightContent:YES];
    
    ByTabBarController *tabBar = (ByTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [tabBar hideTabBar];
    
    if (BY_USER) {
        [self updateUserInfo];
    }
}

#pragma mark - net
- (void)uploadIcon:(UIImage *)icon {
    self.icon = icon;
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.iconImage = icon;
    [ByCoreToolAlert alertLoading:@"正在上传头像..."];
    [self.vm uploadIcon:params];
}
- (void)uploadIconSuccess:(ByNetBaseResult *)result {
    NSLog(@"%@", result.data);
    self.iconView.image = self.icon;
    
    /** 更换用户信息 */
    ByNetUserModel *user = BY_USER;
    user.head = [NSString stringWithFormat:@"%@?id=%@&time=%lf", BOYE_AVATAR_URL, user.uid, [NSDate currentDateTimeInterval]];
    [ByCommonCache setUserInfo:user];
    
    [ByCoreToolAlert alertDismiss];
}
- (void)uploadIconFailure:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertError:result.msg];
    [ByCoreToolAlert alertDismiss];
}



#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.subjects.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = FONT(14);
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.font = FONT(14);
    }
    
    if (indexPath.row == 0) {
        cell.accessoryView = self.iconView;
    } else {
        
        if (indexPath.row == 3) {
            cell.detailTextLabel.text = [self.subjectDetails[indexPath.row] integerValue] == 1 ? @"已认证" : @"未认证";
            cell.detailTextLabel.textColor = [self.subjectDetails[indexPath.row] integerValue] == 1 ? JM_THEME_COLOR : [UIColor lightGrayColor];

        } else {
            cell.detailTextLabel.text = self.subjectDetails[indexPath.row];
        }
    }

    
    cell.textLabel.text = self.subjects[indexPath.row];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 50;
    }
    return 40;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    /** 如果未登录，则统一先去登录 */
    if (!BY_USER) {
        ByLoginVC *vc = [[ByLoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return;
    }
    
    /** 选择头像 */
    if (indexPath.row == 0) {
        
        [self.iconChooseDataSource makeConfigToSelectIcon];
        __weak typeof(self) weakself = self;
        [self.iconChooseDataSource setCompleteBlock:^(UIImage *image) {
            
            [weakself uploadIcon:image];
        }];
    }
    /** 昵称修改 */
    else if (indexPath.row == 1) {
        
        ByPersonalNickModifyVC *vc = [[ByPersonalNickModifyVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];

    }
    /** 修改邮箱 */
    else if (indexPath.row == 2) {
        
        ByPersonalEmailModifyVC *vc = [[ByPersonalEmailModifyVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    /** 认证 */
    else if (indexPath.row == 3) {
        
        ByPersonalAuthenticationVC *vc = [[ByPersonalAuthenticationVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}




#pragma mark - 点击事件




#pragma mark - 辅助方法
- (void)updateUserInfo {
    /** 刷新头像 */
    [self.iconView setImageWithURL:[NSURL URLWithString:BY_USER.head] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
    [self.subjectDetails replaceObjectAtIndex:1 withObject:BY_USER.nickname];
    [self.subjectDetails replaceObjectAtIndex:2 withObject:BY_USER.email];
    [self.subjectDetails replaceObjectAtIndex:3 withObject:BY_USER.is_auth_e];

    [self.table reloadData];
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    NSLog(@"%@ memory warning", [self class]);
}




@end
